﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface IJobService : IBaseService<Job> {
        Job GetNext();
    }

    public class JobService : BaseService, IJobService {
        public IList<Job> GetList(Func<Job, bool> filters) {
            if (filters != null)
                return db.Jobs.Where(filters).ToList();
            else
                return db.Jobs.ToList();
        }

        public Job Find(params object[] keys) {
            return db.Jobs.Find(keys);
        }

        public void Add(Job model) {
            db.Jobs.Add(model);
        }

        public void Update(Job model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Job model) {
            db.Jobs.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public Job GetNext() {
            int id = 1;
            if (db.Jobs.Count() > 0)
                id = db.Jobs.Max(c => c.Id) + 1;
            return new Job { Id = id };
        }
    }
}